using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XnaDebugDrawer
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        public enum Mode
        {
            Lines,
            Circle,
            Rectangle
        }

        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Mode mode;

        private List<Vector2> vertexes;
        const int maxLines = 32;


        private List<Vector2> circleCenterList;
        const int maxCircles = 16;

        private List<Rectangle> rectangleList;
        const int maxRectangles = 8;


        KeyboardState currentKeyboardState;
        KeyboardState previousKeyboardState;

        MouseState currentMouseState;
        MouseState previousMouseState;

        SpriteFont font;

        public Game1()
        {

            graphics = new GraphicsDeviceManager(this);
            graphics.PreferredBackBufferWidth = 700;
            graphics.PreferredBackBufferHeight = 400;

            Content.RootDirectory = "Content";

            vertexes = new List<Vector2>();
            circleCenterList = new List<Vector2>();
            rectangleList = new List<Rectangle>();

            currentKeyboardState = Keyboard.GetState();
            currentMouseState = Mouse.GetState();

            IsMouseVisible = true;
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here

            DebugDrawer.LoadContent(GraphicsDevice);
            font = Content.Load<SpriteFont>("default");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            UpdateInput(gameTime);


            base.Update(gameTime);
        }

        protected void UpdateInput(GameTime gameTime)
        {
            previousKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            previousMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();

            //Enter changes the mode
            if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyUp(Keys.Enter))
                ToogleMode();

            //Esc exists the game
            if (currentKeyboardState.IsKeyDown(Keys.Escape) && previousKeyboardState.IsKeyUp(Keys.Escape))
                Exit();

            //Mouse click destroys texture
            if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released)
            {
                float x = currentMouseState.X;
                float y = currentMouseState.Y;
                if (x > 0 && x < graphics.PreferredBackBufferWidth && y > 0 && y < graphics.PreferredBackBufferHeight)
                    HandleMouseClick(new Vector2(x, y));
            }
                
        }

        public void ToogleMode()
        {
            if (mode == Mode.Circle)
            {
                mode = Mode.Rectangle;
            }
            else if (mode == Mode.Rectangle)
            {
                mode = Mode.Lines;
            }
            else
            {
                mode = Mode.Circle;
            }
        }

        public void HandleMouseClick(Vector2 mousePosition)
        {
            if(mode==Mode.Lines)
            {
                if (vertexes.Count == maxLines)
                    vertexes.RemoveAt(0);

                vertexes.Add(mousePosition);

            }
            else if(mode==Mode.Circle)
            {
                if (circleCenterList.Count == maxCircles)
                    circleCenterList.RemoveAt(0);

                circleCenterList.Add(mousePosition);
            }
            else
            {

                Rectangle rectangle = new Rectangle((int)(mousePosition.X - 20),
                    (int)mousePosition.Y - 20,
                    40,
                    40);


                if (rectangleList.Count == maxRectangles)
                    rectangleList.RemoveAt(0);

                rectangleList.Add(rectangle);
            }

        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();


            DrawRectangle();
            DrawCircles();
            DrawLines();


            spriteBatch.DrawString(font, "Mode: ", new Vector2(30, 30), Color.Red);
            spriteBatch.DrawString(font, mode.ToString(), new Vector2(90, 30), Color.Red);
            spriteBatch.DrawString(font, "Press Enter to change mode.", new Vector2(30, 50), Color.Red);

            spriteBatch.End();

            base.Draw(gameTime);
        }

        private void DrawRectangle()
        {
            for (int i = 0; i < rectangleList.Count; i++)
            {
                spriteBatch.DrawRectangle(rectangleList[i], Color.Orange, 3);
            }
        }

        private void DrawCircles()
        {
            for (int i = 0; i < circleCenterList.Count; i++)
            {
                spriteBatch.DrawCircle(circleCenterList[i], 20, Color.Orange, 3);
            }
        }

        
        private void DrawLines()
        {
            for (int i = 0; i < vertexes.Count - 1; i++)
            {
                spriteBatch.DrawLineSegment(vertexes[i], vertexes[i + 1], Color.Orange, 3);
            }

            if (mode == Mode.Lines && vertexes.Count > 0)
            {
                Vector2 mousePosition = new Vector2(currentMouseState.X, currentMouseState.Y);
                spriteBatch.DrawLineSegment(vertexes[vertexes.Count - 1], mousePosition, Color.OrangeRed, 3);
            }
        }
    }
}
